Post

Replies

Boosts

Views

Activity

Reply to Schedule start of notification requests
class LocalNotificationManager: ObservableObject {          @Environment(\.managedObjectContext) private var viewContext     var meetings: Item?          var notifications = [Notification]()          init() {         UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in             if granted == true && error == nil {                 print("Notifications permitted")             } else {                 print("Notifications not permitted")             }         }     }          func sendNotification(title: String, subtitle: String?, body: String, time: Date, id: UUID, repeatPattern: String) {         let content = UNMutableNotificationContent()         content.title = title         if let subtitle = subtitle {             content.subtitle = subtitle         }         content.body = body         content.sound = UNNotificationSound.default                  let triggerDate = time - 5 * 60         if repeatPattern == "Daily" {             let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.second], from: triggerDate), repeats: true)             let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)             UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)         } else if repeatPattern == "Weekly" {             let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.timeZone, .weekday, .hour, .minute, .second], from: triggerDate), repeats: true)             let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)             UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)         } else if repeatPattern == "Monthly" {             let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.timeZone, .weekOfMonth, .weekday, .hour, .minute, .second], from: triggerDate), repeats: true)             let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)             UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)         } else if repeatPattern == "None" {             let trigger = UNCalendarNotificationTrigger(                 dateMatching: Calendar.current.dateComponents([.timeZone, .year, .month, .day, .hour, .minute], from: triggerDate), repeats: true)             let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)                     UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)         }         let center = UNUserNotificationCenter.current()         center.getPendingNotificationRequests(completionHandler: { requests in             for request in requests {                 print(request)                              }         })     } }
Feb ’21
Reply to Schedule start of notification requests
    func sendNotification(title: String, subtitle: String?, body: String, time: Date, id: UUID, repeatPattern: String) {         let content = UNMutableNotificationContent()         content.title = title         if let subtitle = subtitle {             content.subtitle = subtitle         }         content.body = body         content.sound = UNNotificationSound.default                  let triggerDate = time - 5 * 60         if repeatPattern == "Daily" {             let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.second], from: triggerDate), repeats: true)             let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)             UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)         } else if repeatPattern == "Weekly" {             let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.timeZone, .weekday, .hour, .minute, .second], from: triggerDate), repeats: true)             let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)             UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)         } else if repeatPattern == "Monthly" {             let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.current.dateComponents([.timeZone, .weekOfMonth, .weekday, .hour, .minute, .second], from: triggerDate), repeats: true)             let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)             UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)         } else if repeatPattern == "None" {             let trigger = UNCalendarNotificationTrigger(                 dateMatching: Calendar.current.dateComponents([.timeZone, .year, .month, .day, .hour, .minute], from: triggerDate), repeats: true)             let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)                     UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)         } } Calling the function in the add meeting modal : self.notificationManager.sendNotification(title: "Upcoming Meeting", subtitle: nil, body: "\(meetingName) will start in 5 minutes. Tap for meeting information.", time: newMeeting.time!, id: newMeeting.id!, repeatPattern: "\(repeats)") print("Scheduled repeating notification.")
Feb ’21
Reply to Schedule start of notification requests
I think your second idea is what I might use, because of this: each event a user creates has a UUID stored in Core Data. The notification identifier is the same as the UUID, so when a user deletes an event, it's really simple to delete the pending notification request. Using your method of creating a new notification when the current one fires, can I recycle the same UUID and keep using that UUID for the future notifications? Also, I'm not familiar with how to schedule a new notification when the existing one fires. What function is that?
Feb ’21
Reply to Button not dismissing Onboarding Modal
Seems like it might have been an error with Xcode. I just tried using the simulator, and it works fine. I was using my personal device and deleting the app each time. That might have something to do with the app storage property, I don't know. Do you know if deleting an app from the device actually resets the app storage properties? The 'Welcome to Meetings' still shows up, but the 'get started' button doesn't work.
Feb ’21
Reply to Best practice for removePendingNotificationRequests with CoreData
Code for content view: @Environment(\.managedObjectContext) private var viewContext @FetchRequest(         entity: Item.entity(),         sortDescriptors: [NSSortDescriptor(keyPath: \Item.time, ascending: true)],         animation: .default)     private var items: FetchedResultsItem NavigationView {                 List {                     Section(header:                         Text("One-Time Meetings")                             .textCase(nil)                             .font(.headline)                             .onAppear {                                 checkForUpdate()                             }                     ) {                         ForEach(items, id: \.self) { item in                             OneTimeRow(meeting: item)                         }                         .onDelete(perform: deleteItems, removeSingleNotification(meetingID: "\(item.meetingLink)"))                     }     private func deleteItems(offsets: IndexSet) {         withAnimation {             offsets.map { items[$0] }.forEach(viewContext.delete)             do {                 try viewContext.save()             } catch {                 print("There was an error deleting items")             }         }     }     private func removeSingleNotification(meetingID: String) {         notificationManager.removePendingNotificationRequests(meetingID: meetingID)         print("Deleted notification scheduled for meeting with identifier \(meetingID)")     }
Feb ’21